﻿<?php
/*
 *
 *
 */
require_once(dirname(__FILE__) .'/exception/ConfigException.php');

class Config {
    
    private static $instance;

    var $config = array();
    var $moduleConf = null;
    
    private function __construct() {
        //Todo: check config file existance and throw exception
        $loadResult = require_once(dirname(__FILE__) .'/config/config.php');
        
        foreach ($config as $key => $value){
            	$this->config[$key] = $value;
		$this->$key = $value; // sao lotuspool ko can set lai the nay nhi
	}
        $this->config['site_url'] = $this->config['site_domain'] . $this->config['site_path'];
    }
    
    public static function getInstance() {
        if(!self::$instance) {
            self::$instance = new Config();
        }
        
        return self::$instance;
    }
	
	/**
	 * @author: HieuTV
	 * read config file of module
	 * @param string $modulePath
	 * @return array
	 */
	public static function getModuleConf($moduleName) {

		try {
			$moduleConf = array();
			$paths = PATH_MODULES . $moduleName;
			//$aryRequest =  explode('/', $_SERVER['SCRIPT_NAME']);
			//if ($aryRequest[count($aryRequest)-2] != 'administrator') 
				//$paths = PATH_MODULES . $moduleName . DS . 'admin';
				
			$iniFile = $paths . '/config/constant.ini';
			if (is_file($iniFile)) {
				$moduleConf = @parse_ini_file($iniFile, true);
			}
			
			return $moduleConf;

		} catch (Exception $e) {
			throw new Exception('Module constant file not found! - '.$iniFile);
		}
	}
	/**
	 * check is path of administrator on url
	 * @author: HieuTV
	 * @return true if administrator else return false
	 * tuantt update call method by module is not construct
	 * 31.12.2010
	 */
	public function getLinkDefault($moduleName) {
		$aryModule = array('module_name'=>$mod);
		$moduleName = ucfirst($moduleName);

		$objMCtrl = new $moduleName($aryModule);

		$aryMethod = get_class_methods($objMCtrl);
		
		if (method_exists($objMCtrl, 'createUrl') && @in_array('createUrl', $aryMethod)) {
			$aryUrl = $objMCtrl->createUrl();
		}
/*
		$aryMethod = get_class_methods($moduleName);
		//print_r($aryMethod);
		if (@in_array($aryMethod, 'createUrl')) {
			$aryUrl = ModuleManager::runFunctionOfModule($moduleName, 'createUrl');
			print_r($aryUrl);
		}
*/
		return $aryUrl;
	}
	/**
	 * check is path of administrator on url
	 * @author: HieuTV
	 * @return true if administrator else return false
	 */
	public function checkPathAdmin() {
		$aryRequest =  explode('/', $_SERVER['SCRIPT_NAME']);
		if ($aryRequest[count($aryRequest)-2] != 'administrator') 
			return false;
		else
			return true;
	}

    function __get($name) {
        if (array_key_exists($name, $this->config))
            return $this->config[$name];
        else
            throw new ConfigException('Can not find config value for: '. $name);
    }

}
?>
